const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
const fs = require("fs");

const writeFile = (event, data) => {
  fs.writeFileSync("D:/hello.txt", data);
};

const readFile = () => {
  const fls = fs.readFileSync("D:/hello.txt").toString();
  console.log(fls);
  return fls;
};

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    autoHideMenuBar: true,
    webPreferences: {
      // preload: path.resolve(__dirname, "./preload.js"),
      nodeIntegration: true,
      // enableRemoteModule: true,
    },
  });
  win.on("close", () => {
    win = null;
  });
  ipcMain.on("file-save", writeFile);
  ipcMain.handle("file-read", readFile);
  win.loadFile("./pages/index.html");
  win.on("ready-to-show", () => {
    win.show();
  });
};

app.on("ready", () => {
  console.log(2222333);
  createWindow();

  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

app.on("window-all-closed", () => {
  app.quit();
});
